package org.example.com.service.impl;

import org.example.com.mapper.HouseMapper;
import org.example.com.mapper.ReservationsMapper;
import org.example.com.pojo.Reservations;
import org.example.com.pojo.Result;
import org.example.com.pojo.vo.ReservationsVO;
import org.example.com.service.ReservationsService;
import org.example.com.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ReservationsServiceImpl implements ReservationsService {
    @Autowired
    private ReservationsMapper reservationsMapper;
    @Autowired
    private HouseMapper houseMapper;
    @Override
    public int submit(Reservations reservations) {
        //补充属性值
        reservations.setCreateAt(Timestamp.valueOf(LocalDateTime.now()));
        reservations.setUpdatedAt(Timestamp.valueOf(LocalDateTime.now()));
        //userId获取
        Map<String,Object> map= ThreadLocalUtil.get();
        System.out.println(map);
//        int userId = 1;
        int userId = (Integer)map.get("id");
        reservations.setUserId(userId);
        reservations.setStatus("待看房");
        //访问数据库
        reservationsMapper.submit(reservations);
        return reservations.getId();
    }

    @Override
    public List<ReservationsVO> history() {
        //获取当前用户信息
        Map<String,Object> claims = ThreadLocalUtil.get();
        int userId = (int)claims.get("id");
        List<Reservations> reservationsList= reservationsMapper.history(userId);
        List<ReservationsVO> reservationsVOList = new ArrayList<>();
        int number = reservationsList.size(); //订单数量
        for(int i=0;i<number;i++){
            ReservationsVO reservationsVO = new ReservationsVO();
            reservationsVO.setId(reservationsList.get(i).getId()); //设置订单id
            reservationsVO.setDate(reservationsList.get(i).getDate()); //设置订单创建时间
            reservationsVO.setHouseId(reservationsList.get(i).getHouseId()); //设置房源id

            /*
            House
             */
            int houseId = reservationsList.get(i).getHouseId();
            reservationsVO.setHousePicture(houseMapper.findById(houseId).getHousePicture());//to do
            reservationsVO.setHouseTitle(houseMapper.findById(houseId).getHouseTitle()); //to do
            reservationsVO.setAddress(houseMapper.findById(houseId).getAddress());
            reservationsVO.setDistanceInfo(houseMapper.findById(houseId).getDistanceInfo());
            reservationsVO.setRentPriceListing(houseMapper.findById(houseId).getRentPriceListing());
            reservationsVO.setRentPriceUnit(houseMapper.findById(houseId).getRentPriceUnit());
            reservationsVO.setTags(houseMapper.findById(houseId).getTags());
            reservationsVO.setComment(reservationsList.get(i).getComment());
            reservationsVO.setStatus(reservationsList.get(i).getStatus());
            reservationsVO.setCreatedAt(reservationsList.get(i).getCreateAt());
            reservationsVOList.add(reservationsVO);
        }
        return reservationsVOList;
    }

    @Override
    public void delete(int id) {
        reservationsMapper.delete(id);
    }
}
